﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class testonline : System.Web.UI.Page
{
    private static string ExamCode = "", round = "";    
    private static int SELECT_QUESTION  = 0;
    private SQLQuery sqlQuery = null;

    private static int min = 30;
    private static int sen = 59;
    private static bool isRuntime = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["candidate"] == null)
        {
            Response.Redirect("index.aspx");
        }
        if (Request.QueryString["examcode"] != null)
        {
            ExamCode = Request.QueryString["examcode"].Trim();;
            lbl_examcode.Text = ExamCode;
        }
        if (Request.QueryString["examcode"] != null)
        {
            round = Request.QueryString["round"].Trim();
        }
        sqlQuery = new SQLQuery();
    }

    protected void lbtn_ques_01_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 1;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_02_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 2;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_03_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 3;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_04_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 4;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_05_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 5;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_06_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 6;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_07_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 7;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_08_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 8;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_09_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 9;
        clearAnswer();
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();
    }

    protected void lbtn_ques_10_Click(object sender, EventArgs e)
    {
        SELECT_QUESTION = 10;
        clearAnswer();  
        setTextQuestion(SELECT_QUESTION);
        isCheckAnswer();   
    }

    private void setTextQuestion(int count)
    {
        isRuntime = true;
        lbl_question.Text = "Question " + count;
        int index = count - 1;
        lbl_content_question.Text = LocalVariables.CONTENT[index].Trim();
    }

    private void clearAnswer()
    {
        rb_a.Checked = false;
        rb_b.Checked = false;
        rb_c.Checked = false;
        rb_d.Checked = false;
    }

    protected void btn_begin_Click(object sender, EventArgs e)
    {
        min = 29;
        sen = 59;
        lbl_runtime.Visible = true;
        btn_begin.Enabled = false;
        btn_completed.Enabled = true;
        enabledButton();
    }

    private void enabledButton()
    {
        lbtn_ques_01.Enabled = true;
        lbtn_ques_02.Enabled = true;
        lbtn_ques_03.Enabled = true;
        lbtn_ques_04.Enabled = true;
        lbtn_ques_05.Enabled = true;
        lbtn_ques_06.Enabled = true;
        lbtn_ques_07.Enabled = true;
        lbtn_ques_08.Enabled = true;
        lbtn_ques_09.Enabled = true;
        lbtn_ques_10.Enabled = true;
        rb_a.Enabled = true;
        rb_b.Enabled = true;
        rb_c.Enabled = true;
        rb_d.Enabled = true;
    }

    protected void rb_a_CheckedChanged(object sender, EventArgs e)
    {
        saveAnswer("A");
    }

    protected void rb_b_CheckedChanged(object sender, EventArgs e)
    {
        saveAnswer("B");
    }

    protected void rb_c_CheckedChanged(object sender, EventArgs e)
    {
        saveAnswer("C");
    }

    protected void rb_d_CheckedChanged(object sender, EventArgs e)
    {
        saveAnswer("D");
    }

    private void saveAnswer(string answer)
    {
        int index = SELECT_QUESTION - 1;
        LocalVariables.ANSWER[index] = answer;
        switch (SELECT_QUESTION)
        {
            case 1:
                lbtn_ques_01.Text = "Question 01 - " + answer;
                break;
            case 2:
                lbtn_ques_02.Text = "Question 02 - " + answer;
                break;
            case 3:
                lbtn_ques_03.Text = "Question 03 - " + answer;
                break;
            case 4:
                lbtn_ques_04.Text = "Question 04 - " + answer;
                break;
            case 5:
                lbtn_ques_05.Text = "Question 05 - " + answer;
                break;
            case 6:
                lbtn_ques_06.Text = "Question 06 - " + answer;
                break;
            case 7:
                lbtn_ques_07.Text = "Question 07 - " + answer;
                break;
            case 8:
                lbtn_ques_08.Text = "Question 08 - " + answer;
                break;
            case 9:
                lbtn_ques_09.Text = "Question 09 - " + answer;
                break;
            case 10:
                lbtn_ques_10.Text = "Question 10 - " + answer;
                break;
        }
    }

    private void isCheckAnswer()
    {
        int index = SELECT_QUESTION - 1;
        if (LocalVariables.ANSWER[index] != null)
        {
            if (LocalVariables.ANSWER[index].Trim().Equals("A"))
            {
                rb_a.Checked = true;
            }
            else if (LocalVariables.ANSWER[index].Trim().Equals("B"))
            {
                rb_b.Checked = true;
            }
            else if (LocalVariables.ANSWER[index].Trim().Equals("C"))
            {
                rb_c.Checked = true;
            }
            else if (LocalVariables.ANSWER[index].Trim().Equals("D"))
            {
                rb_d.Checked = true;
            }
        }
    }

    protected void btn_completed_Click(object sender, EventArgs e)
    {
        completedExam();        
    }

    private void completedExam()
    {
        isRuntime = false;
        btn_completed.Enabled = false;
        disabledButton();
        string status = "";
        int results = calculatorResult();
        if (results > 6)
        {
            status = "Pass";
        }
        else
        {
            status = "Fails";
        }
        lbl_results.Text = "Results : " + results + "/10 ( " + status + " )";

        string answer = "";
        for (int i = 0; i < LocalVariables.ANSWER.Length; i++)
        {
            if (i == 0)
            {
                answer += LocalVariables.ANSWER[i].Trim();
            }
            else
            {
                answer += "," + LocalVariables.ANSWER[i].Trim();
            }
        }

        sqlQuery.updateTested(Session["candidate"].ToString(), round, 1, answer, Convert.ToString(results), status);
    }

    private int calculatorResult()
    {
        int result = 0;
        for (int i = 0; i < LocalVariables.ANSWER.Length; i++)
        {
            if (LocalVariables.ANSWER[i].Trim().Equals(LocalVariables.ANSWER_TRUE[i].Trim()))
            {
                result++;
            }
        }
        return result;
    }

    private void disabledButton()
    {
        lbtn_ques_01.Enabled = false;
        lbtn_ques_02.Enabled = false;
        lbtn_ques_03.Enabled = false;
        lbtn_ques_04.Enabled = false;
        lbtn_ques_05.Enabled = false;
        lbtn_ques_06.Enabled = false;
        lbtn_ques_07.Enabled = false;
        lbtn_ques_08.Enabled = false;
        lbtn_ques_09.Enabled = false;
        lbtn_ques_10.Enabled = false;
        rb_a.Enabled = false;
        rb_b.Enabled = false;
        rb_c.Enabled = false;
        rb_d.Enabled = false;
    }

    protected void mTimer_Tick(object sender, EventArgs e)
    {
        if (isRuntime)
        {
            lbl_runtime.Text = "Remaining time [ " + min + ":" + sen + " ]";
            sen--;
            if (min == 0 && sen == 0)
            {
                completedExam();
                isRuntime = false;
                return;
            }
            if (sen < 0)
            {
                sen = 59;
                min--;
            }
        }
    }
}